﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ClientApp.Classes;
using GianosNET.Common.Classes;

namespace ClientApp.Gestori
{
    partial class GestoreGenerico : Base, ILoopingServiceGestore
    {
        #region Eventi
        
        public event ServiceCommandHandler OnLoopingForceStart;
        public event CommonHandlers.LoopingPauseChanged OnLoopingPauseChanged;
        public event CommonHandlers.RequireFullLogHandler RequireFullLog;

        #endregion

        public GestoreGenerico()
        {
            InitializeComponent();
        }

        internal override void SetServiceInformation(ServiceInfoExt info)
        {
            base.SetServiceInformation(info);

            LogQueue lQ;

            // Create multi-tabbed logs
            foreach (KeyValuePair<Guid, string> g in info.ServiceLogId)
            {
                TabPage tp = new TabPage(g.Value ?? "") {Tag = g.Key};
                TextBox tb = new TextBox()
                                 {
                                     WordWrap = true,
                                     AutoSize = false,
                                     Dock = DockStyle.Fill,
                                     ScrollBars = ScrollBars.Vertical,
                                     Multiline = true
                                 };
                tp.Controls.Add(tb);
                tcLog.TabPages.Add(tp);

                // carica il log preesistente
                lQ = RequireFullLog != null ? RequireFullLog(g.Key) : null;
                if (lQ != null)
                    tb.Text = lQ.ToString(Environment.NewLine);
            }

            ucLoopingStatus.Info = info;
            ucLoopingStatus.LoopingPauseChanged += ucLoopingStatus_LoopingPauseChanged;
            ucLoopingStatus.LoopingForceStart += ucLoopingStatus_LoopingForceStart;
            // Ripristina il log delle exception
            lQ = RequireFullLog != null ? RequireFullLog(info.SvcId) : null;
            if (lQ != null)
                tbLogExc.Text = lQ.ToString(Environment.NewLine);

        }

        public override void AppendToLog(Guid logId, string msg)
        {
            if (logId == info.SvcId)
                tbLogExc.AppendText(msg + Environment.NewLine);
            else if (info.ServiceLogId.ContainsKey(logId))
            {
                foreach (TabPage page in tcLog.TabPages)
                    if (page.Tag is Guid && (Guid) page.Tag == logId)
                    {
                        TextBox tb = page.Controls[0] as TextBox;
                        if (tb != null)
                            tb.AppendText(msg + Environment.NewLine);
                        break;
                    }
            }
        }

        public override void ChangeLoopingStatus(Guid id, LoopingService.LoopingStateEnum status)
        {
            base.ChangeLoopingStatus(id, status);

            switch(status)
            {
                case LoopingService.LoopingStateEnum.Running :
                    ucLoopingStatus.Text = "In esecuzione";
                    ucLoopingStatus.Idle = false;

                    break;
                case LoopingService.LoopingStateEnum.Stopped:
                    ucLoopingStatus.Text = "Non attivo";
                    ucLoopingStatus.Idle = false;

                    break;
                case LoopingService.LoopingStateEnum.Idle:
                    ucLoopingStatus.Text = "In attesa prossima esecuzione";
                    ucLoopingStatus.Idle = true;

                    break;
                default:
                    ucLoopingStatus.Text = "";
                    ucLoopingStatus.Idle = false;
                    break;
            }
        }

        public override void ChangeIdleCountdown(Guid id, int secBeforeNextExecution)
        {
            base.ChangeIdleCountdown(id, secBeforeNextExecution);

            ucLoopingStatus.Text = string.Format("In attesa prossima esecuzione.. {0} sec.", secBeforeNextExecution);
            ucLoopingStatus.Idle = secBeforeNextExecution > 0;
        }

       

        private void ucLoopingStatus_LoopingPauseChanged(Guid svcId, int newLoopingPause)
        {
            if (OnLoopingPauseChanged != null)
                OnLoopingPauseChanged(svcId, newLoopingPause);
        }

        void ucLoopingStatus_LoopingForceStart(Guid svcId)
        {
            if (OnLoopingForceStart != null)
                OnLoopingForceStart(this, new ServiceEventArgs(svcId));
        }
    }
}
